package com.example.beautymakeup.service.impl;

import com.example.beautymakeup.dao.BannerDao;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.pojo.Banner;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.service.BannerService;
import com.example.beautymakeup.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerDao bannerDao;

    @Override
    public PageBean<Banner> get(String token) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Banner> hots = bannerDao.get();
            int count = bannerDao.getCount();
            PageBean<Banner> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(hots);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Banner> getAll(String token, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            List<Banner> all = bannerDao.getAll((page - 1) * size, size);
            int allCount = bannerDao.getAllCount();
            PageBean<Banner> pageBean = new PageBean<>();
            pageBean.setRows(all);
            pageBean.setTotalCount(allCount);
            return pageBean;
        }
        return null;
    }

    @Override
    public boolean update(String token, Banner banner) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return bannerDao.update(banner) == 1;
        }
        return false;
    }

    @Override
    public boolean add(String token, Banner banner) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return bannerDao.add(banner) == 1;
        }
        return false;
    }

    @Override
    public boolean ban(String token, Banner banner) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return bannerDao.ban(banner) == 1;
        }
        return false;
    }

    @Override
    public boolean delete(String token, Banner banner) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return bannerDao.delete(banner) == 1;
        }
        return false;
    }
}
